package com.example.zhaoping.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zhaoping.entity.Message;
import com.example.zhaoping.entity.MessageList;
import com.example.zhaoping.entity.PageParam;
import com.example.zhaoping.mapper.MessageMapper;
import com.example.zhaoping.service.MessageSerice;
import net.minidev.json.writer.UpdaterMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MessageServiceImpl implements MessageSerice {
    @Resource
    private MessageMapper messageMapper = null;

    @Override
    public int insterMessage(Message message) {
        System.out.println(message);
        return messageMapper.insert(message);
    }

    @Override
    public Page<Message> selectListMessage(String fromType, String toType, int fromId, int toId, PageParam param) {
        Page<Message> page = new Page<>(param.getPageNum(),param.getPageSize());
        page.addOrder(OrderItem.asc("createTime"));
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fromType",fromType)
                .eq("toType",toType)
                .eq("fromId",fromId)
                .eq("toId",toId);
        Page msgList = messageMapper.selectPage(page,queryWrapper);
        return msgList;
    }

    @Override
    public int selectMsgCount(String type, int id) {
        QueryWrapper<Message> queryWrapper = new QueryWrapper();
        queryWrapper.eq("readed",0).eq("toId",id).eq("toType",type);
        return messageMapper.selectCount(queryWrapper);
    }

    @Override
    public int setReaded(int formId, String formType, int toId, String toType) {
        UpdateWrapper<Message> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("readed",1)
                .eq("fromType",formType)
                .eq("fromId",formId)
                .eq("toType",toType)
                .eq("toId",toId)
                .eq("readed",0);
        return messageMapper.update(null,updateWrapper);
    }

    public int getReaded(int toId, String toType,int formId, String formType){
        QueryWrapper<Message> queryWrapper = new QueryWrapper();
        queryWrapper.eq("readed",0)
                .eq("toId",toId)
                .eq("toType",toType)
                .eq("fromId",formId)
                .eq("fromType",formType);
        return messageMapper.selectCount(queryWrapper);
    }

    public List selectMsgList(String type, int id) {
        QueryWrapper<Message> queryWrapper = new QueryWrapper();
        queryWrapper.eq("toId",id).eq("toType",type);
        List<MessageList> messageLists = new ArrayList<>();
        List<Message> messages = messageMapper.selectList(queryWrapper);
        for(Message m : messages){
            MessageList m1 = new MessageList();
            m1.setFromId(m.getFromId());
            m1.setFromName(m.getFromName());
            m1.setFromAvatar(m.getFromAvatar());
            m1.setFromType(m.getFromType());
            m1.setContent(m.getContent());
            m1.setTime(m.getCreateTime());
            messageLists.add(m1);
        }
        Map<Integer,List<MessageList>> m = messageLists.stream().collect(Collectors.groupingBy(e->e.getFromId()));
        List list = new ArrayList();
        m.forEach((key,value)->{
            Map<String,Object> map = new HashMap<>();
            map.put("MessageList",value);
            int count = this.getReaded(id,type,
                    value.get(0).getFromId(),
                    value.get(0).getFromType());
            map.put("readCount",count);
            list.add(map);
        });
        return list;
    }
}
